package com.mdp.im.plugins;

import com.alibaba.fastjson.JSONObject;
import com.mdp.im.core.plugin.api.PublishPlugin;
import com.mdp.im.core.utils.msg.SubType;
import com.mdp.mq.queue.Push;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

@Service
public class WxpubPushPlugin implements PublishPlugin {

    @Autowired
    Push push;

    @Override
    public boolean beforePublish(JSONObject message) {
        return true;
    }

    @Override
    public void afterPublish(boolean isGo, JSONObject message) {
        if(isGo==true){
            String subTypes=message.getString("subTypes");
            if(!StringUtils.hasText(subTypes)){
                return;
            }

            if(subTypes.indexOf(SubType.wxpub)<0){
                return;
            }
            doSendWxpubTemplateMsg(message);
        }

    }

    private void doSendWxpubTemplateMsg(JSONObject message) {
        push.leftPush("mdpim_to_wxpub_tplmsg",message);
    }
}
